﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using System.Reflection;

namespace YiYiTong.Rocket.Helper
{
    /// <summary>
    /// log4net 日志帮助类
    /// </summary>
    public class LogHelper
    {
        /// <summary>
        /// log全局变量
        /// 名称与配置文件中logger name的名称一致
        /// </summary>
        private static readonly ILog logger = LogManager.GetLogger("Rocket.Log");

        public static void Debug(string message)
        {
            logger.Debug(message);
        }

        public static void Info(string message)
        {
            logger.Info(GetMessage(message));
        }

        public static void Error(string message)
        {
            logger.Error(message);
        }

        public static void Fatal(string message)
        {
            logger.Fatal(message);
        }

        private static string GetMessage(string message)
        {
            //获取调用者类名 1代表上级，2代表上上级，以此类推
            try
            {
                MethodBase method = new System.Diagnostics.StackTrace().GetFrame(2).GetMethod();
                string className = method.ReflectedType.FullName;
                string methodName = method.Name;

                message = message + "\t\n类名：" + className + "\t\n方法名：" + methodName;
                return message;
            }
            catch (Exception e) { return message; }
        }
    }
}
